﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace AMM.Forms.Reports {
    public partial class FrmReportSearch : FrmRootBase {

        #region Search event

        public delegate void ReportSearchEventHandler(object sender, SearchArgs e);
        public event ReportSearchEventHandler Search;
        public void OnSearch(object sender, SearchArgs e) {
            if (this.Search != null) {
                this.Search(sender, e);
            }
        }

        public class SearchArgs : EventArgs {
            public object Datasource { get; set; }
            public string ReportName { get; set; }
        }

        #endregion

        public FrmReportSearch() {
            this.InitializeComponent();
        }

        private BaseReportSearchControl GetSearchControl() {
            BaseReportSearchControl searchControl = new BaseReportSearchControl();
            foreach (Control c in this.Controls) {
                if (c is BaseReportSearchControl) {
                    searchControl = (BaseReportSearchControl)c;
                    break;
                }
            }
            return searchControl;
        }

        private void btnSearch_Click(object sender, EventArgs e) {
            // preventing close when any one of buttons clicked
            this.DialogResult = DialogResult.None;

            object ds = null;
            string reportName = string.Empty;

            //BaseReportSearchControl searchControl = this.GetSearchControl();
            //ds = searchControl.FetchDatasource();
            //reportName = searchControl.ReportName;
            //this.OnSearch(this, new SearchArgs() {
            //    Datasource = ds,
            //    ReportName = reportName
            //});

            this.CreateNewThread(() => {
                BaseReportSearchControl searchControl = this.GetSearchControl();
                ds = searchControl.FetchDatasource();
                reportName = searchControl.ReportName;
            }, () => {
                this.OnSearch(this, new SearchArgs() {
                    Datasource = ds,
                    ReportName = reportName
                });
                this.Close();
            });
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            // preventing close when any one of buttons clicked
            this.DialogResult = DialogResult.None;
            this.GetSearchControl().ResetSearch();
        }

        private void FrmReportSearch_ControlAdded(object sender, ControlEventArgs e) {
            BaseReportSearchControl searchControl = this.GetSearchControl();
            this.Bounds = searchControl.Bounds;
            searchControl.Location = new Point(0, 10);
            this.Height += this.pnlBtn.Height * 2 + 30;
            this.Width = (this.Width <= this.pnlBtn.Width ? this.pnlBtn.Width : this.Width) + 20;
            this.pnlBtn.Location = new Point((searchControl.Width - this.pnlBtn.Width) / 2, searchControl.Top + searchControl.Height + 10);
        }

    }
}
